#include "dataareader.h"

#include "dataaevent.h"
#include "datareader.h"
#include "startedevent.h"
#include "workdoneevent.h"

#include <chrono>
#include <memory>

namespace Lab1
{
    const std::chrono::milliseconds DataAReader::ResponseTime(1000);
    DataAReader::DataAReader( DataReader * reader )
        : reader( reader )
    {
    }

    void DataAReader::start( int iterations, bool errorEnd )
    {
        std::shared_ptr < Event > event ( new StartedEvent( Event::DEVICE_A ) );

        reader->getQueue().push( event );

        sleepThread();

        for( int i = 0; i < iterations; i++ )
        {
            read();
        }

        if( errorEnd )
        {
            event = std::shared_ptr < Event > ( new WorkDoneEvent( Event::DEVICE_A ) );
            reader->getQueue().push( event );
        }
    }

    void DataAReader::read()
    {
        std::string str;
        std::string tempString("data " );
        while(str.size()+tempString.size() < 500)
        {
            str.append(tempString);
        }
        std::shared_ptr < Event > event ( new DataAEvent(str) );
        reader->getQueue().push( event );
        sleepThread();
    }

    void DataAReader::sleepThread()
    {
         std::this_thread::sleep_for(ResponseTime);
    }


}
